<?php
use com\gdl\common\dataobject\common\ResultDO; 
use com\gdl\kq\interfaces\AttAppApprovalServiceIf;
use com\gdl\kq\dataobject\attapproval\ApprovalAllDTO; 
use com\gdl\kq\dataobject\attapproval\ApprovalDTO;  
 
require(dirname(dirname(__FILE__)) ."/php_sdk-master/include.php");
use MagicCrypt\MagicCrypt;
/**
 * 考勤系统App审批记录接口 此为公共接口
 * 本接口实现的核心类为 /service/approcess.class.php 
 */
class api_AttAppApprovalService extends api_Abstract implements AttAppApprovalServiceIf{
	
	# 默认留底照片
	static public $photo = "";
	 
	//审批流程  提交申请页面
	//1请假流程 2外出流程 3补卡流程 5加班流程
	public function approvalMember(ApprovalDTO $approval){
		#接受参数
		$userId = $approval->userId ? gdl_lib_BaseUtils::getStr($approval->userId) : "";
		$platformId = $approval->platformId ? gdl_lib_BaseUtils::getStr($approval->platformId) : "";
		$unitId = $approval->unitId ? gdl_lib_BaseUtils::getStr($approval->unitId) : "";
		$data = $approval->data ? gdl_lib_BaseUtils::getStr($approval->data) : ""; 
		$type = $data['type'] ? $data['type'] : "";
		
		# 实例化
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		#验证
		if(empty($type)){$result->message = "请选择类型"; return $result;}
		
		try{
			#获取审批流程规则
			$appService = new service_appprocess();
			$rule = $appService->getMemberProcess($userId, $platformId, $type);
			if(empty($rule['rules'])){$result->message = "用户还未加入任何审批流程，请联系管理员添加"; return $result;}
			#重组审批流程 传入userId 和一个序列化的审批流程规则
			$recomdRule = $appService->actionApprocess($userId, $rule['rules']);
			if(empty($recomdRule)){$result->message = "未找到相应审批人，请联系管理员添加"; return $result;}			
			
			$newList = [];
			$j=0;
			for($i=0; $i<=count($recomdRule); $i++){
				if(!empty($recomdRule[$i]['user'])){ 
					$newList[$j]['user'] = $recomdRule[$i]['user'];
					$newList[$j]['level'] = $recomdRule[$i]['level'];
					$newList[$j]['levelName'] = $recomdRule[$i]['levelName'];
					$j++;
				}
			}
			
			if($newList){
				$info['rule'] = json_encode($newList);
				$result->message = ""; 
				$result->code = 200;
				$result->data[] = $info;
			}else{
				$result->message = "请先添加审批人";
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
        return $result;
	}
	
	//获取审核状态流程 申请已完成查看审批流程状态 第一视角
	//1请假流程 2外出流程 3补卡流程 5加班流程
	public function approvalStatus(ApprovalDTO $approval){
		#接受参数
		$userId = $approval->userId ? gdl_lib_BaseUtils::getStr($approval->userId) : "";
		$platformId = $approval->platformId ? gdl_lib_BaseUtils::getStr($approval->platformId) : "";
		$unitId = $approval->unitId ? gdl_lib_BaseUtils::getStr($approval->unitId) : "";
		$data = $approval->data ? gdl_lib_BaseUtils::getStr($approval->data) : ""; 
		$type = $data['type'] ? $data['type'] : "";
		$recordId = $data['recordId'] ? $data['recordId'] : ""; #申请id	
		# 实例化
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		#验证
		if(empty($type)){$result->message = "请选择类型"; return $result;}
		if(empty($recordId)){$result->message = "请选择审批记录"; return $result;}
		if(empty($userId)){$result->message = "请选择发起人"; return $result;}
		
		try{
			#获取审批流程状态
			$appService = new service_appprocess();
			$rule = $appService->approvalStatus($userId, $recordId, $type); #当前用户 申请id 类型
			if($rule){
				$info['data'] = json_encode($rule);
				$result->message = "获取成功";
				$result->code = 200;
				$result->data[]= $info;
			}else{
				$result->message = "该审批流程已变更，撤销该申请后请重新提交申请";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result;
	}
	// 审批流程状态 第三方视角 
	public function showApprovalStatus(ApprovalDTO $approval){
		#接受参数
		$userId = $approval->userId ? gdl_lib_BaseUtils::getStr($approval->userId) : "";
		$platformId = $approval->platformId ? gdl_lib_BaseUtils::getStr($approval->platformId) : "";
		$unitId = $approval->unitId ? gdl_lib_BaseUtils::getStr($approval->unitId) : "";
		$data = $approval->data ? gdl_lib_BaseUtils::getStr($approval->data) : ""; 
		$type = $data['type'] ? $data['type'] : "";
		$recordId = $data['recordId'] ? $data['recordId'] : ""; #申请id	
		# 实例化
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		#验证
		if(empty($type)){$result->message = "请选择类型"; return $result;}
		if(empty($recordId)){$result->message = "请选择审批记录"; return $result;}
		
		try{
			#获取审批流程状态
			$appService = new service_appprocess();
			$rule = $appService->showApprovalStatus($recordId, $type); #当前用户 申请id 类型
			if($rule){
				$info['data'] = json_encode($rule);
				$result->message = "获取成功";
				$result->code = 200;
				$result->data[]= $info;
			}else{
				$result->message = "请先添加审批人";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		 
        return $result;
	}
	//$rule = $appService->editApproval(5,15,2); #当前用户 申请id 类型
	
	// 我的申请记录列表 外出 请假 补卡全在一起
	public function applyList(ApprovalAllDTO $approval){
		#接受参数
		$userId = $approval->userId ? gdl_lib_BaseUtils::getStr($approval->userId) : "";
		$platformId = $approval->platformId ? gdl_lib_BaseUtils::getStr($approval->platformId) : "";
		$unitId = $approval->unitId ? gdl_lib_BaseUtils::getStr($approval->unitId) : "";
		$offset = $approval->offset ? gdl_lib_BaseUtils::getStr($approval->offset) : 1;
		$num = $approval->num ? gdl_lib_BaseUtils::getStr($approval->num) : 10;
		$data = $approval->data ? gdl_lib_BaseUtils::getStr($approval->data) : ""; 
		$likeTitle = $data['likeTitle'] ? $data['likeTitle']:""; #模糊查询标题
		$status = $data['status'] ? $data['status'] :"0"; # 状态 0：全部 1：审批完成 2：审批中 3：撤销
		$type = $data['type'] ? $data['type']:0; # 1:请假 2：外出 3：补卡 5：加班

		# 实例化
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		#验证
		try{
			#层级表加三个申请表的所有记录 为三个关联查询 
			$levelModel = new model_attendance_approvalprocessleaverecord(); #请假
			$outgoModel = new model_attendance_appoutgorecord(); #外出
			$cardModel = new model_attendance_approvalprocesscardreplacementrecord(); #补卡
			$overModel = new model_attendance_overtimerecord(); #加班model
			$userModel = new model_attendance_member(); #用户model
			
			#查询用户信息
			$userWhere = "id={$userId}";
			$userFiled = "id,realName,photo";
			$userInfo = $userModel->selectOne($userWhere, $userFiled);
			$userInfo['photo'] = $userInfo['photo']?$userInfo['photo']:'';
			
			#type赛选
			$levelList = [];
			$outgolList = [];
			$cardlList = [];
			$overList = [];
			
			#请假
			$levelWhere = "memberId={$userId} ";
			$outgoWhere = "memberId={$userId} "; 
			$cardWhere = "memberId={$userId} ";
			$overWhere = "memberId={$userId}";
			if($likeTitle){
				$levelWhere.=" and (numberId like '%".$likeTitle."%' or leaveInfo like '%".$likeTitle."%') ";
				$outgoWhere.=" and (numberId like '%".$likeTitle."%' or outGo like '%".$likeTitle."%') ";
				$cardWhere.=" and (numberId like '%".$likeTitle."%' or reason like '%".$likeTitle."%') ";
				$overWhere.=" and (numberId like '%".$likeTitle."%' or overTimeInfo like '%".$likeTitle."%') ";
			}
			if($status==1){
				$levelWhere.=" and (status=2 or status=3) ";
				$outgoWhere.=" and (status=2 or status=3) ";
				$cardWhere.=" and (status=2 or status=3) ";
				$overWhere.=" and (status=2 or status=3) ";
			}
			if($status==2){
				$levelWhere.=" and status=1 ";
				$outgoWhere.=" and status=1 ";
				$cardWhere.=" and status=1 ";
				$overWhere.=" and status=1 ";
			}
			if($status==3){
				$levelWhere.=" and status=4 ";	
				$outgoWhere.=" and status=4 ";	
				$cardWhere.=" and status=4 ";
				$overWhere.=" and status=4 ";
			}
			$levelFiled = "id,numberId,memberId,FROM_UNIXTIME(createTime, '%Y.%m.%d %H:%i') as createTime,createTime as creates,leaveInfo,numbers,FROM_UNIXTIME(endTime, '%Y.%m.%d %H:%i') as endTime,FROM_UNIXTIME(startTime,'%Y.%m.%d %H:%i') as startTime,leaveType,status";
			$outgoFiled = "id,status,numberId,numbers,outGo,FROM_UNIXTIME(startTime, '%Y.%m.%d %H:%i') as startTime,FROM_UNIXTIME(endTime, '%Y.%m.%d %H:%i') as endTime, FROM_UNIXTIME(createTime, '%Y.%m.%d %H:%i') as createTime,createTime as creates";
			$cardFiled = "id,reason,numberId,FROM_UNIXTIME(createTime, '%Y.%m.%d %H:%i') as createTime,createTime as creates,replacementTime,baseType,status,sureTime";
			$overFiled = "id,numberId,memberId,FROM_UNIXTIME(createTime, '%Y.%m.%d %H:%i') as createTime,createTime as creates,overTimeInfo,numbers,FROM_UNIXTIME(endTime, '%Y.%m.%d %H:%i') as endTime,FROM_UNIXTIME(startTime,'%Y.%m.%d %H:%i') as startTime,status";
			if($type==1){				
				$levelList = $levelModel->select($levelWhere, $levelFiled)->items;
			}
			if($type==2){
				#外出
				$outgoList = $outgoModel->select($outgoWhere, $outgoFiled)->items;
			}
			if($type==3){
				#补卡	
				$cardList = $cardModel->select($cardWhere, $cardFiled)->items;
			}
			if($type==5){
				#加班
				$overList = $overModel->select($overWhere, $overFiled)->items;
			}
			if($type==0){
				#请假
				$levelList = $levelModel->select($levelWhere, $levelFiled)->items;
				#外出
				$outgoList = $outgoModel->select($outgoWhere, $outgoFiled)->items;
				#补卡
				$cardList = $cardModel->select($cardWhere, $cardFiled)->items;
				#加班
				$overList = $overModel->select($overWhere, $overFiled)->items;
			}
			
			$newList = [];
			#type 1:请假 2：外出 3：补卡 5：加班 把四条数据揉成一起
			for($j=0; $j<count($levelList); $j++){
				$levelList[$j]['type'] = 1; 
				
				$newList[] = $levelList[$j];
			}
			for($k=0;$k<count($outgoList);$k++){
				$outgoList[$k]['type'] = 2;
				$newList[] = $outgoList[$k];
			}
			for($y=0;$y<count($cardList);$y++){
				$cardList[$y]['type'] = 3;
				$newList[] = $cardList[$y];
			}
			for($z=0;$z<count($overList);$z++){
				$overList[$z]['type'] = 5;
				$newList[] = $overList[$z];
			}
			#排序
			$sort = [];
			foreach($newList as $v){
				$sort[] = $v['numberId'];
			}
			array_multisort($sort,SORT_DESC,$newList);
			
			#自己的分页算法
			$startPage = intval($offset-1)*$num; # 分页 i开始
			$endPage = $num;
			if($offset>1){
				$endPage = intval($offset*$num);
			}
			#分页 重组数据
			$resultList = [];
			for($i=$startPage; $i<$endPage; $i++){
				if($newList[$i]){
					if($newList[$i]['type']==1){ #请假
						$newList[$i]['approvalUser'] = $this->getLeaveMember($newList[$i]['id'], $userId); #审批者信息
					}
					if($newList[$i]['type']==2){ #外出
						$newList[$i]['approvalUser'] = $this->getOutgoMember($newList[$i]['id'], $userId); #审批者信息
					}
					if($newList[$i]['type']==3){# 补卡
						$newList[$i]['approvalUser'] = $this->getCardMember($newList[$i]['id'], $userId); #审批者信息
					}
					if($newList[$i]['type']==5){# 加班
						$newList[$i]['approvalUser'] = $this->getOverMember($newList[$i]['id'], $userId); #审批者信息
					}
					#$newList[$i]['userInfo'] = $userInfo;
					$resultList[] = $newList[$i];
				}
			}
			#最后返回数组
			$endRet = [];
			#再次遍历一遍 重组数据
			for($xy=0;$xy<count($resultList); $xy++){
				if($resultList[$xy]['type']==1){
					$endRet[$xy] = [
						'id'=>$resultList[$xy]['id'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'userInfo'=>$userInfo,
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1审核中 2审核通过 3未通过 4撤销
						'leaveType'=>$resultList[$xy]['leaveType'],
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['startTime']],
						'endTime'=>$resultList[$xy]['endTime']?$resultList[$xy]['endTime']:"",
						'info'=>$resultList[$xy]['leaveInfo']?$resultList[$xy]['leaveInfo']:"", #原因
					];
				}
				if($resultList[$xy]['type']==2){
					$endRet[$xy] = [
						'id'=>$resultList[$xy]['id'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'userInfo'=>$userInfo,
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1审核中 2审核通过 3未通过 4撤销
						'leaveType'=>"",
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['startTime']],
						'endTime'=>$resultList[$xy]['endTime']?$resultList[$xy]['endTime']:"",
						'info'=>$resultList[$xy]['outGo']?$resultList[$xy]['outGo']:"", #原因
					];
				}
				if($resultList[$xy]['type']==3){
					$today = $this->getToday(strtotime($resultList[$xy]['replacementTime']));
					$cardType = "上班时间：18:00";
					if($resultList[$xy]['baseType']==1){
						$cardType = "上班时间：".$resultList[$xy]['sureTime'];
					}
					if($resultList[$xy]['baseType']==2){
						$cardType = "下班时间：".$resultList[$xy]['sureTime'];
					}
					$endRet[$xy] = [
						'id'=>$resultList[$xy]['id'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'userInfo'=>$userInfo,
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1审核中 2审核通过 3未通过 4撤销
						'leaveType'=>"",
						'baseType'=>$resultList[$xy]['baseType'],#假期类型
						'startTime'=>[$resultList[$xy]['replacementTime'],$today,$cardType],
						'endTime'=>"",
						'info'=>$resultList[$xy]['reason']?$resultList[$xy]['reason']:"", #原因
					];
				}
				
				if($resultList[$xy]['type']==5){
					$endRet[$xy] = [
						'id'=>$resultList[$xy]['id'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'userInfo'=>$userInfo,
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1审核中 2审核通过 3未通过 4撤销
						'leaveType'=>"",
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['startTime']],
						'endTime'=>$resultList[$xy]['endTime']?$resultList[$xy]['endTime']:"",
						'info'=>$resultList[$xy]['overTimeInfo']?$resultList[$xy]['overTimeInfo']:"", #原因
					];
				}

			}
			
			if($endRet){
				$r['list'] = json_encode($endRet);
				$result->message = "获取成功";
				$result->code = 200;
				$result->data[] = $r;
			}else{
				$result->code = 200;
				$result->message = "暂无记录";
			}
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		return $result;
	}
	// 我的审批记录列表 外出 请假 加班补卡全在一起
	public function approvalList(ApprovalAllDTO $approval){
		#接受参数
		$userId = $approval->userId ? gdl_lib_BaseUtils::getStr($approval->userId) : "";
		$platformId = $approval->platformId ? gdl_lib_BaseUtils::getStr($approval->platformId) : "";
		$unitId = $approval->unitId ? gdl_lib_BaseUtils::getStr($approval->unitId) : "";
		$offset = $approval->offset ? gdl_lib_BaseUtils::getStr($approval->offset) : 1;
		$num = $approval->num ? gdl_lib_BaseUtils::getStr($approval->num) : 10;
		$data = $approval->data ? gdl_lib_BaseUtils::getStr($approval->data) : ""; 
		$likeTitle = $data['likeTitle'] ? $data['likeTitle']:""; #模糊查询标题
		$status = $data['status'] ? $data['status'] :"0"; # 状态 0：全部 1：待我审批 2：我已审批 
		$type = $data['type'] ? $data['type']:0; # 1:请假 2：外出 3：补卡 5:加班
		$adopt = $data['adopt']?$data['adopt']:0; #0全部 1：审批完成 2：审批中 3：撤销

		# 实例化
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		
		#三个空数组
		$levelList = [];
		$outgoList = [];
		$cardList = [];
		$overList = [];
		
		try{
			#层级表加三个申请表的所有记录 为三个关联查询
			$recordModel = new model_attendance_approvalrecord();
			
			#我的所有请假
			$leaveLeftJoin = [
				'atdc_approval_process_leave_record as le' => 'atdc_approve_record.recordId=le.id',
			];
			#我的所有外出
			$outgoLeftJoin = [
				'atdc_app_outgo_record as le' => 'atdc_approve_record.recordId=le.id',
			];
			#我的所有补卡
			$cardLeftJoin = [
				'atdc_approval_process_card_replacement_record as le' => 'atdc_approve_record.recordId=le.id',
			];
			#我的加班审批
			$overLeftJoin = [
				'atdc_overtime_record as le' => 'atdc_approve_record.recordId=le.id',
			];
			
			#查询条件
			$leaveWhere = "atdc_approve_record.memberId={$userId} and atdc_approve_record.isShow=1 and type=1 ";
			$outgoWhere = "atdc_approve_record.memberId={$userId} and atdc_approve_record.isShow=1 and type=2 ";
			$cardWhere = "atdc_approve_record.memberId={$userId} and atdc_approve_record.isShow=1 and type=3 ";
			$overWhere = "atdc_approve_record.memberId={$userId} and atdc_approve_record.isShow=1 and type=5 ";
			if($likeTitle){
				$leaveWhere.=" and (le.numberId like '%".$likeTitle."%' or le.leaveInfo like '%".$likeTitle."%') ";
				$outgoWhere.=" and (le.numberId like '%".$likeTitle."%' or le.outGo like '%".$likeTitle."%') ";
				$cardWhere.=" and (le.numberId like '%".$likeTitle."%' or le.reason like '%".$likeTitle."%') ";
				$overWhere.=" and (le.numberId like '%".$likeTitle."%' or le.overTimeInfo like '%".$likeTitle."%') ";
			}
			#查询整条信息的状态 我已审批赛选
			if($adopt){
				if($status==1){ #待我审批
					$leaveWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
					$outgoWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
					$cardWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
					$overWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
				}
				if($status==2){ #我已审批的
					if($adopt==1){
						$leaveWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and (le.status=2 or le.status=3) ";#124通过撤回
						$outgoWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and (le.status=2 or le.status=3)";#124通过撤回
						$cardWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and (le.status=2 or le.status=3)";#124通过撤回
						$overWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and (le.status=2 or le.status=3)";#124通过撤回
					}
					if($adopt==2){
						$leaveWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=1 ";#124通过撤回
						$outgoWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=1";#124通过撤回
						$cardWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=1";#124通过撤回
						$overWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=1";#124通过撤回
					}
					if($adopt==3){
						$leaveWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=4";#124通过撤回
						$outgoWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=4";#124通过撤回
						$cardWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=4";#124通过撤回
						$overWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) and le.status=4";#124通过撤回
					}
					if($adopt==0 || empty($adopt)){
						$leaveWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
						$outgoWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
						$cardWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
						$overWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
					}
				}
			}else{
				if($status==1){
					$leaveWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
					$outgoWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
					$cardWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
					$overWhere.=" and atdc_approve_record.adopt=1 and le.status=1";
				}
				if($status==2){
					$leaveWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
					$outgoWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
					$cardWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
					$overWhere.=" and (atdc_approve_record.adopt=3 or atdc_approve_record.adopt=2 or atdc_approve_record.adopt=4 ) ";#124通过撤回
				}
			}
			#我已审批赛选
			
			
			#需要字段
			$levelFiled = "atdc_approve_record.id as cardId,atdc_approve_record.adopt,le.status,FROM_UNIXTIME(atdc_approve_record.createTime, '%Y.%m.%d %H:%i') as createTime,atdc_approve_record.createTime as creates,le.leaveType,le.leaveInfo,FROM_UNIXTIME(le.startTime, '%Y.%m.%d %H:%i') as startTime,FROM_UNIXTIME(le.endTime, '%Y.%m.%d %H:%i') as endTime,le.id as recordId,atdc_approve_record.coverMemberId as userId";
			$outgoFiled = "atdc_approve_record.id as cardId,atdc_approve_record.adopt,le.status,FROM_UNIXTIME(atdc_approve_record.createTime, '%Y.%m.%d %H:%i') as createTime,atdc_approve_record.createTime as creates,le.outGo,FROM_UNIXTIME(le.startTime, '%Y.%m.%d %H:%i') as startTime,FROM_UNIXTIME(le.endTime, '%Y.%m.%d %H:%i') as endTime,le.id as recordId,atdc_approve_record.coverMemberId as userId";
			$cardFiled = "atdc_approve_record.id as cardId,atdc_approve_record.adopt,le.status,FROM_UNIXTIME(atdc_approve_record.createTime, '%Y.%m.%d %H:%i') as createTime,atdc_approve_record.createTime as creates,le.replacementTime,le.sureTime,le.baseType,le.reason,le.id as recordId, atdc_approve_record.coverMemberId as userId";
			$overFiled = "atdc_approve_record.id as cardId,atdc_approve_record.adopt,le.status,FROM_UNIXTIME(atdc_approve_record.createTime, '%Y.%m.%d %H:%i') as createTime,atdc_approve_record.createTime as creates,le.overTimeInfo,FROM_UNIXTIME(le.startTime, '%Y.%m.%d %H:%i') as startTime,FROM_UNIXTIME(le.endTime, '%Y.%m.%d %H:%i') as endTime,le.id as recordId,atdc_approve_record.coverMemberId as userId";
			
			#请假
			if($type==1){				
				$levelList = $recordModel->select($leaveWhere, $levelFiled,'','',$leaveLeftJoin)->items;
			}
			#外出
			if($type==2){
				$outgoList = $recordModel->select($outgoWhere, $outgoFiled,'','',$outgoLeftJoin)->items;
			}
			#补卡
			if($type==3){
				$cardList = $recordModel->select($cardWhere, $cardFiled,'','',$cardLeftJoin)->items;
			}
			#加班
			if($type==5){
				$overList = $recordModel->select($overWhere, $overFiled,'','',$overLeftJoin)->items;
			}
			if($type==0){
				#请假审批
				$levelList = $recordModel->select($leaveWhere, $levelFiled,'','',$leaveLeftJoin)->items;
				#外出审批
				$outgoList = $recordModel->select($outgoWhere, $outgoFiled,'','',$outgoLeftJoin)->items;
				#补卡
				$cardList = $recordModel->select($cardWhere, $cardFiled,'','',$cardLeftJoin)->items;
				#加班
				$overList = $recordModel->select($overWhere, $overFiled,'','',$overLeftJoin)->items;
			}
			
			# 总条数
			$listCount = count($levelList)+count($outgoList)+count($cardList)+count($overList);
			$result->counts = $listCount?$listCount:0;
			
			$newList = [];
			#type 1:请假 2：外出 3：补卡 5加班
			for($j=0; $j<count($levelList); $j++){
				$levelList[$j]['type'] = 1; 
				$newList[] = $levelList[$j];
			}
			for($k=0;$k<count($outgoList);$k++){
				$outgoList[$k]['type'] = 2;
				$newList[] = $outgoList[$k];
			}
			for($y=0;$y<count($cardList);$y++){
				$cardList[$y]['type'] = 3;
				$newList[] = $cardList[$y];
			}
			for($z=0;$z<count($overList);$z++){
				$overList[$z]['type'] = 5;
				$newList[] = $overList[$z];
			}
			
			#排序
			$sort = [];
			foreach($newList as $v){
				$sort[] = $v['creates'];
			}
			array_multisort($sort,SORT_DESC,$newList);
			
			#自己的分页算法
			$startPage = intval($offset-1)*$num; # 分页 i开始
			$endPage = $num;
			if($offset>1){
				$endPage = intval($offset*$num);
			}
			#分页
			$resultList = [];
			for($i=$startPage; $i<$endPage; $i++){
				if($newList[$i]){
					if($newList[$i]['type']==1){ #请假
						$newList[$i]['approvalUser'] = $this->getLeaveMember($newList[$i]['recordId'], $newList[$i]['userId']); #审批者信息
					}
					if($newList[$i]['type']==2){ #外出
						$newList[$i]['approvalUser'] = $this->getOutgoMember($newList[$i]['recordId'], $newList[$i]['userId']); #审批者信息
					}
					if($newList[$i]['type']==3){# 补卡
						$newList[$i]['approvalUser'] = $this->getCardMember($newList[$i]['recordId'], $newList[$i]['userId']); #审批者信息
					}
					if($newList[$i]['type']==5){# 加班
						$newList[$i]['approvalUser'] = $this->getOverMember($newList[$i]['recordId'], $newList[$i]['userId']); #审批者信息
					}
					$resultList[] = $newList[$i];
				}
			}
			
			#最后返回数组
			$endRet = [];
			#再次遍历一遍 重组数据
			for($xy=0;$xy<count($resultList); $xy++){
				if($resultList[$xy]['type']==1){
					$endRet[$xy] = [
						'recordId'=>$resultList[$xy]['recordId'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'cardId'=>$resultList[$xy]['cardId'],
						'userInfo'=>$this->getUserInfo($resultList[$xy]['userId']),
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1:待审批 2：通过 3:tongguo 4:撤回
						'myStatus'=>$resultList[$xy]['adopt'],#1:待审批 2：通过 3:tongguo 4:撤回
						'leaveType'=>$resultList[$xy]['leaveType'],
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['startTime']],
						'endTime'=>$resultList[$xy]['endTime']?$resultList[$xy]['endTime']:"",
						'info'=>$resultList[$xy]['leaveInfo']?$resultList[$xy]['leaveInfo']:"", #原因
					];
				}
				if($resultList[$xy]['type']==2){
					$endRet[$xy] = [
						'recordId'=>$resultList[$xy]['recordId'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'cardId'=>$resultList[$xy]['cardId'],
						'userInfo'=>$this->getUserInfo($resultList[$xy]['userId']),
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1:待审批 2：通过 3:tongguo 4:撤回
						'myStatus'=>$resultList[$xy]['adopt'],#1:待审批 2：通过 3:tongguo 4:撤回
						'leaveType'=>"",
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['startTime']],
						'endTime'=>$resultList[$xy]['endTime']?$resultList[$xy]['endTime']:"",
						'info'=>$resultList[$xy]['outGo']?$resultList[$xy]['outGo']:"", #原因
					];
				}
				if($resultList[$xy]['type']==3){
					$today = $this->getToday(strtotime($resultList[$xy]['replacementTime']));
					$cardType = "上班时间：18:00";
					if($resultList[$xy]['baseType']==1){
						$cardType = "上班时间：".$resultList[$xy]['sureTime'];
					}
					if($resultList[$xy]['baseType']==2){
						$cardType = "下班时间：".$resultList[$xy]['sureTime'];
					}
					$endRet[$xy] = [
						'recordId'=>$resultList[$xy]['recordId'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'cardId'=>$resultList[$xy]['cardId'],
						'userInfo'=>$this->getUserInfo($resultList[$xy]['userId']),
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1:待审批 2：通过 3:tongguo 4:撤回
						'myStatus'=>$resultList[$xy]['adopt'],#1:待审批 2：通过 3:tongguo 4:撤回
						'leaveType'=>"",
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['replacementTime'],$today,$cardType],
						'endTime'=>"",
						'info'=>$resultList[$xy]['reason']?$resultList[$xy]['reason']:"", #原因
					];
				}
				
				if($resultList[$xy]['type']==5){
					$endRet[$xy] = [
						'recordId'=>$resultList[$xy]['recordId'],
						'approvalUser'=>$resultList[$xy]['approvalUser']?$resultList[$xy]['approvalUser']:[],
						'cardId'=>$resultList[$xy]['cardId'],
						'userInfo'=>$this->getUserInfo($resultList[$xy]['userId']),
						'createTime'=>$resultList[$xy]['createTime'],
						'type'=>$resultList[$xy]['type'],
						'status'=>$resultList[$xy]['status'],#1:待审批 2：通过 3:tongguo 4:撤回
						'myStatus'=>$resultList[$xy]['adopt'],#1:待审批 2：通过 3:tongguo 4:撤回
						'leaveType'=>"",
						'baseType'=>"",#假期类型
						'startTime'=>[$resultList[$xy]['startTime']],
						'endTime'=>$resultList[$xy]['endTime']?$resultList[$xy]['endTime']:"",
						'info'=>$resultList[$xy]['overTimeInfo']?$resultList[$xy]['overTimeInfo']:"", #原因
					];
				}
			}
			if($endRet){
				$r['list'] = json_encode($endRet);
				$result->message = "获取成功";
				$result->code = 200;
				$result->data[] = $r; 
			}else{
				$result->code = 200;
				$result->message = "暂无数据";
			}
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		
		return $result;
	}
	
	// 审批申请 
	public function examineApproval(ApprovalDTO $approval){
		#接受参数
		$userId = $approval->userId ? gdl_lib_BaseUtils::getStr($approval->userId) : "";
		$platformId = $approval->platformId ? gdl_lib_BaseUtils::getStr($approval->platformId) : "";
		$unitId = $approval->unitId ? gdl_lib_BaseUtils::getStr($approval->unitId) : "";
		$data = $approval->data ? gdl_lib_BaseUtils::getStr($approval->data) : ""; 
		$cardId = $data['cardId'] ? $data['cardId'] :""; #审批层级表 id
		$info = $data['info'] ? $data['info'] :"未填写"; #审批意见
		$status = $data['status'] ? $data['status']:""; #1:拒绝 2：通过
		$type = $data['type']?$data['type']:"";#1：请假审批流程 2：外出审批流程 3：补卡审批流程 
		# 实例化
		$result = new ResultDO();
        $result->success = true;
        $result->code = 400; 
        $result->data = [];
		//var_dump($type);exit;
		#验证
		try{ 
			$appModel = new model_attendance_approvalrecord(); #层级model
			#查询id
			$where = "id={$cardId}";
			$recordStatus = $appModel->selectOne($where, "adopt");
			if($recordStatus['adopt']==2){
				$result->message = "审批已通过，请勿重复操作";
				return $result;
			}
			if($recordStatus['adopt']==3){
				$result->message = "审批已拒绝，请勿重复操作";
				return $result;
			}
			if($recordStatus['adopt']==4){
				$result->message = "审批已撤销，请勿重复操作";
				return $result;
			}
			
			#再查询
			$appService = new service_appprocess();
			$ret = $appService->editApproval($cardId, $status, $info, $type); #层级表ID 状态 意见 type
			
			if($status==1){$result->message = "已拒绝该申请";}
			if($status==2){$result->message = "审核已通过";}
			$result->code = 200;
			
		}catch(Exception $ex){
			$result->message = $ex->getMessage();
		}
		return $result;
	}
	
	//遍历找审批人 补卡审批人 补卡id 补卡 被审批人userID
	private function getCardMember($recordId,$userId, $type=3){
		
		$user = [];
		
		$cardModel = new model_attendance_approvalprocesscardreplacementrecord(); 
		$userModel = new model_attendance_member();
		
		$leftJoin = [
			'atdc_approve_record as r' => 'atdc_approval_process_card_replacement_record.id=r.recordId',
		];
		$where = " r.id= (select MAX(rs.level) from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type}) and r.adopt=1 "; #可见并且最大审批层 and coverMemberId=zijiand MAX(r.level) r.isShow=1 and r.type=1 and
		
		#查找 审核状态为待审批3 被审批人为当前审批人 记录id为当前记录id 并且显示的审批分层id
		$where = " r.id= (select id from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type} and rs.adopt=1 and rs.coverMemberId={$userId} and rs.recordId={$recordId} GROUP BY level DESC LIMIT 1) ";
		$recordInfo = $cardModel->select($where, 'r.id,r.coverMemberId,r.memberId,r.adopt', '', '', $leftJoin)->items;
		
		if($recordInfo){
			for($j=0; $j<count($recordInfo); $j++){
				$userWhere = "id={$recordInfo[$j]['memberId']}";
				$userFiled = "id,realName";
				$userInfo = $userModel->selectOne($userWhere, $userFiled);
				$userInfo['approvalId'] = $recordInfo[$j]['id']?$recordInfo[$j]['id']:"0";
				$myId = $userInfo['id']?$userInfo['id']:"0";
				$myName = $userInfo['realName']?$userInfo['realName']:"无";
				$user[] = ['id'=>$myId, 'realName'=>$myName, 'approvalId'=>$userInfo['approvalId']];//$userInfo;
			}
		} 
		return $user;
	}
	// 加班
	private function getOverMember($recordId, $userId, $type=5){
		$user = [];
		
		$overModel = new model_attendance_overtimerecord(); 
		$userModel = new model_attendance_member();
		
		$leftJoin = [
			'atdc_approve_record as r' => 'atdc_overtime_record.id=r.recordId',
		];
		$where = " r.id= (select MAX(rs.level) from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type}) and r.adopt=1 "; #可见并且最大审批层 and coverMemberId=zijiand MAX(r.level) r.isShow=1 and r.type=1 and
		
		#查找 审核状态为待审批3 被审批人为当前审批人 记录id为当前记录id 并且显示的审批分层id
		$where = " r.id= (select id from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type} and rs.adopt=1 and rs.coverMemberId={$userId} and rs.recordId={$recordId} GROUP BY level DESC LIMIT 1) ";
		$recordInfo = $overModel->select($where, 'r.id,r.coverMemberId,r.memberId,r.adopt', '', '', $leftJoin)->items;
		
		if($recordInfo){
			for($j=0; $j<count($recordInfo); $j++){
				$userWhere = "id={$recordInfo[$j]['memberId']}";
				$userFiled = "id,realName";
				$userInfo = $userModel->selectOne($userWhere, $userFiled);
				$userInfo['approvalId'] = $recordInfo[$j]['id']?$recordInfo[$j]['id']:"0";
				$myId = $userInfo['id']?$userInfo['id']:"0";
				$myName = $userInfo['realName']?$userInfo['realName']:"无";
				$user[] = ['id'=>$myId, 'realName'=>$myName, 'approvalId'=>$userInfo['approvalId']];//$userInfo;
			}
		} 
		return $user;
	}
	
	//找单个审批人 外出申请id 
	private function getOutgoMember($recordId,$userId, $type=2){
		
		$user = [];
		
		$outgoModel = new model_attendance_appoutgorecord(); 
		$userModel = new model_attendance_member();
		
		$leftJoin = [
			'atdc_approve_record as r' => 'atdc_app_outgo_record.id=r.recordId',
		];
		$where = " r.id= (select MAX(rs.level) from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type}) and r.adopt=1 "; #可见并且最大审批层 and coverMemberId=zijiand MAX(r.level) r.isShow=1 and r.type=1 and
		
		#查找 审核状态为待审批3 被审批人为当前审批人 记录id为当前记录id 并且显示的审批分层id
		$where = " r.id= (select id from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type} and rs.adopt=1 and rs.coverMemberId={$userId} and rs.recordId={$recordId} GROUP BY level DESC LIMIT 1) ";
		$recordInfo = $outgoModel->select($where, 'r.id,r.coverMemberId,r.memberId,r.adopt', '', '', $leftJoin)->items;
		if($recordInfo){
			for($j=0; $j<count($recordInfo); $j++){
				$userWhere = "id={$recordInfo[$j]['memberId']}";
				$userFiled = "id,realName";
				$userInfo = $userModel->selectOne($userWhere, $userFiled);
				$userInfo['approvalId'] = $recordInfo[$j]['id']?$recordInfo[$j]['id']:"0";
				$myId = $userInfo['id']?$userInfo['id']:"0";
				$myName = $userInfo['realName']?$userInfo['realName']:"无";
				$user[] = ['id'=>$myId, 'realName'=>$myName, 'approvalId'=>$userInfo['approvalId']];//$userInfo;
			}
		} 
		return $user;
	}
	//找单个审批人 请假申请id
	private function getLeaveMember($recordId,$userId, $type=1){
		
		$user = [];
		
		$leaveModel = new model_attendance_approvalprocessleaverecord(); 
		$userModel = new model_attendance_member();
		
		$leftJoin = [
			'atdc_approve_record as r' => 'atdc_approval_process_leave_record.id=r.recordId',
		];
		$where = " r.id= (select MAX(rs.level) from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type}) and r.adopt=1 "; #可见并且最大审批层 and coverMemberId=zijiand MAX(r.level) r.isShow=1 and r.type=1 and
		
		#查找 审核状态为待审批3 被审批人为当前审批人 记录id为当前记录id 并且显示的审批分层id
		$where = " r.id= (select id from atdc_approve_record as rs where rs.isShow=1 and rs.type={$type} and rs.adopt=1 and rs.coverMemberId={$userId} and rs.recordId={$recordId} GROUP BY level DESC LIMIT 1) ";
		$recordInfo = $leaveModel->select($where, 'r.id,r.coverMemberId,r.memberId,r.adopt', '', '', $leftJoin)->items;
		
		if($recordInfo){
			for($j=0; $j<count($recordInfo); $j++){
				$userWhere = "id={$recordInfo[$j]['memberId']}";
				$userFiled = "id,realName";
				$userInfo = $userModel->selectOne($userWhere, $userFiled);
				$userInfo['approvalId'] = $recordInfo[$j]['id']?$recordInfo[$j]['id']:"0";
				$myId = $userInfo['id']?$userInfo['id']:"0";
				$myName = $userInfo['realName']?$userInfo['realName']:"无";
				$user[] = ['id'=>$myId, 'realName'=>$myName, 'approvalId'=>$userInfo['approvalId']];//$userInfo;
			}
		} 
		return $user;
	}
	//根据日期计算星期几
	private function getToday($time){ 
		$i=0;
		$weekarray = array("日","一", "二", "三", "四", "五", "六");
		$oneD = 24 * 60 * 60;  
		return "星期" . $weekarray[date("w", $time + $oneD * $i)];
	}
	//根据userid查找被审批人信息
	private function getUserInfo($userId){
		if(empty($userId)) return false;
		$userModel = new model_attendance_member();
		$where = "id={$userId}";
		$filed = "id,realName,photo";
		$userInfo = $userModel->selectOne($where, $filed);
		return $userInfo?$userInfo:[];
	}
}

